//
//  UIView+LayoutMethods.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/3/30.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "UIView+LayoutMethods.h"

@implementation UIView (LayoutMethods)
- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}


- (void)setX:(CGFloat)x
{
    self.frame =CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}

- (void)setY:(CGFloat)y
{
    self.frame =CGRectMake(self.frame.origin.x, y, self.frame.size.width, self.frame.size.height);
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (CGSize)size
{
    return self.frame.size;
}

- (CGPoint)origin
{
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (CGFloat)bottom
{
    return self.frame.size.height + self.frame.origin.y;
}

- (CGFloat)right
{
    return self.frame.size.width + self.frame.origin.x;
}

- (NSValue*)firstRectFrame
{
    return self.firstRectFrame;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left
{
    self.x =left;
}

- (void)setTop:(CGFloat)top
{
    self.y =top;
}

- (CGFloat)top
{
    return self.frame.origin.y;
}

// height
- (void)setHeight:(CGFloat)height
{
    CGRect newFrame =CGRectMake(self.x, self.y, self.width, height);
    self.frame =newFrame;
}

- (void)heightEqualToView:(UIView *)view
{
    self.height =view.height;
}

// width
- (void)setWidth:(CGFloat)width
{
    CGRect newFrame =CGRectMake(self.x, self.y, width, self.height);
    self.frame =newFrame;
}

- (void)widthEqualToView:(UIView *)view
{
    self.width =view.width;
}

// center
- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center =CGPointMake(self.centerX, self.centerY);
    center.x =centerX;
    self.center =center;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center =CGPointMake(self.centerX, self.centerY);
    center.y =centerY;
    self.center =center;
}

- (void)centerXEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewCenterPoint =[superView convertPoint:view.center toView:self.topSuperView];
    CGPoint centerPoint =[self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
    self.centerX =centerPoint.x;
}

- (void)centerYEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewCenterPoint =[superView convertPoint:view.center toView:self.topSuperView];
    CGPoint centerPoint =[self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
    self.centerY =centerPoint.y;
}

- (void)centerEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewCenterPoint =[superView convertPoint:view.center toView:self.topSuperView];
    CGPoint centerPoint =[self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
    self.centerX =centerPoint.x;
    self.centerY =centerPoint.y;
}




- (void)topEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewOrigin =[superView convertPoint:view.origin toView:self.topSuperView];
    CGPoint newOrigin =[self.topSuperView convertPoint:viewOrigin toView:self.superview];
    
    self.y =newOrigin.y;
}

- (void)bottomEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewOrigin =[superView convertPoint:view.origin toView:self.topSuperView];
    CGPoint newOrigin =[self.topSuperView convertPoint:viewOrigin toView:self.superview];
    
    self.y =newOrigin.y + view.height - self.height;
}

- (void)leftEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewOrigin =[superView convertPoint:view.origin toView:self.topSuperView];
    CGPoint newOrigin =[self.topSuperView convertPoint:viewOrigin toView:self.superview];
    
    self.x =newOrigin.x;
}

- (void)rightEqualToView:(UIView *)view
{
    UIView *superView =view.superview ? view.superview : view;
    CGPoint viewOrigin =[superView convertPoint:view.origin toView:self.topSuperView];
    CGPoint newOrigin =[self.topSuperView convertPoint:viewOrigin toView:self.superview];
    
    self.x =newOrigin.x + view.width - self.width;
}

// size
- (void)setSize:(CGSize)size
{
    self.frame =CGRectMake(self.x, self.y, size.width, size.height);
}

- (void)sizeEqualToView:(UIView *)view
{
    self.frame =CGRectMake(self.x, self.y, view.width, view.height);
}
- (UIView *)topSuperView
{
    UIView *topSuperView =self.superview;
    
    if (topSuperView ==nil) {
        topSuperView =self;
    } else {
        while (topSuperView.superview) {
            topSuperView =topSuperView.superview;
        }
    }
    
    return topSuperView;
}
@end
